<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['allocation:wxContacts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['allocation:wxContacts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['allocation:wxContacts:remove']"
        >删除</el-button>
      </el-col>
       <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['allocation:wxContacts:import']"
            >导入</el-button>
          </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['allocation:wxContacts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wxContactsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="企业名称" align="center" prop="companyName" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.companyName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="'性别" align="center" prop="sex" >
       <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      
      <el-table-column label="会内职务" align="center" prop="internalJob" />
      <el-table-column label="企业职务" align="center" prop="companyJob" />
      <el-table-column label="社会职务" align="center" prop="societyJob" />
      <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true">
         <template slot-scope="scope">
          <span>{{scope.row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column label="办公电话" align="center" prop="phone" />
      <el-table-column label="手机" align="center" prop="mobile" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="会员类型" align="center" prop="vipType">
         <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_platform_type" :value="scope.row.vipType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch  v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="0"
                :inactive-value="2"
                @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['allocation:wxContacts:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['allocation:wxContacts:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自动认证会员数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
         <el-form-item label="会员类型" prop="vipType">
           <el-radio v-for="(item,index) in vipTypeOptions" :key="index" v-model="form.vipType" :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-form-item>
        <el-form-item label="企业名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入团体/企业名称" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
           <el-radio v-for="item in dict.type.sys_user_sex" :key="item.value" v-model="form.sex" :label="item.value">{{item.label}}</el-radio>
        </el-form-item>
         <el-form-item label="会员通讯录分类" prop="internalJob">
          <el-input v-model="form.internalJob" placeholder="请输入会内职务" />
        </el-form-item>
        <el-form-item label="会内职务" prop="internalJob">
          <el-input v-model="form.internalJob" placeholder="请输入会内职务" />
        </el-form-item>
        <el-form-item label="企业职务" prop="companyJob">
          <el-input v-model="form.companyJob" placeholder="请输入企业职务" />
        </el-form-item>
        <el-form-item label="社会职务" prop="societyJob">
          <el-input v-model="form.societyJob" placeholder="请输入社会职务" />
        </el-form-item>
         <el-form-item label="办公电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入办公电话" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address"/>
           <div style="width:100%;border: 2px solid #ced4da;padding: 2px;margin: 10px 0">
              <iframe id="mapPage" style="width: 100%;height: 600px;z-index: 999" frameborder=0
                    src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=5OVBZ-27I6W-5LNRB-OWUIN-FUCVF-5SF6P&referer=云社群-H5">
              </iframe>
              </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
       <!-- 微信榜单导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWxContacts, getWxContacts, delWxContacts, addWxContacts, updateWxContacts,getVipRadio } from "@/api/allocation/wxContacts";
import { getToken } from "@/utils/auth";
export default {
  name: "WxContacts",
   dicts: ['sys_member_audit_type','sys_user_type','sys_platform_type','sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自动认证会员数据表格数据
      wxContactsList: [],
      // 弹出层标题
      title: "",
      vipTypeOptions:[],
        // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/allocation/wxContacts/importData"
      },
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        asId: null,
        companyName: null,
        name: null,
        namePy: null,
        sex: null,
        internalJob: null,
        companyJob: null,
        societyJob: null,
        address: null,
        phone: null,
        mobile: null,
        email: null,
        vipType: null,
        isOld: null,
        openid: null,
        status: null,
        tradeId: null,
        addrCoords: null,
        detailedAddress: null,
        articleType: null,
        contactsType: null
      },
      mainVm:{},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  mounted(){
      //接收来自其他页面的消息并判断发送者
    window.addEventListener("message", this.onMessage);

},
  created() {
    this.getList();
  },
  methods: {
    createIndex(index){
      return (this.queryParams.pageNum - 1) *this.queryParams.pageSize +index +1;
    },
     //获取地址
    onMessage(event) {
	  // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
	  var loc = event.data;
	  if (loc && loc.module == 'locationPicker') {
      //防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
	      console.log('location', loc);
        if(loc.poiname=='我的位置'){
          this.$modal.confirm("请重新选择地址")
        }
        this.form.address=loc.cityname+"·"+loc.poiname;
        let obj={};
        obj.name=loc.poiname;
        obj.coord=loc.lat+","+loc.lng;
        this.form.addrCoords=JSON.stringify(obj);
        console.info(this.form.address);
	      this.mainVm.dialogFormVisibleMap=false;
	  }
},
    /** 查询自动认证会员数据列表 */
    getList() {
      this.loading = true;
      listWxContacts(this.queryParams).then(response => {
        this.wxContactsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.id + '"公告吗？').then(function() {
        return changeNoticeStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "2" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        asId: null,
        companyName: null,
        name: null,
        namePy: null,
        sex: null,
        internalJob: null,
        companyJob: null,
        societyJob: null,
        address: null,
        phone: null,
        mobile: null,
        email: null,
        vipType: null,
        isOld: null,
        openid: null,
        createTime: null,
        updateTime: null,
        status: 0,
        tradeId: null,
        addrCoords: null,
        detailedAddress: null,
        articleType: null,
        contactsType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getVipRadio().then(res=>{
     
      this.vipTypeOptions=res.data;
       console.info(this.vipTypeOptions);
      this.reset();
      this.open = true;
      this.title = "添加微信通讯录数据";     
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWxContacts(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信通讯录数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxContacts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxContacts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
          /** 下载模板操作 */
    importTemplate() {
      this.download('allocation/wxContacts/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
     /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
       // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
     // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
     // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信通讯录数据编号为"' + ids + '"的数据项？').then(function() {
        return delWxContacts(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('allocation/wxContacts/export', {
        ...this.queryParams
      }, `wxContacts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style >

 .el-tooltip__popper{
    max-width:20%;
  }
  .el-tooltip__popper,.el-tooltip__popper.is-dark{
    background:rgb(27, 100, 190) !important;
    color: #fff !important;
    line-height: 20px;
    font-size: 15px;
  }
</style>
